import xlwt
import xlrd
from xlutils.copy import copy


class Salary():
    def __init__(self,_filename):
        self.filename = _filename
        self.old_excel = xlrd.open_workbook(self.filename,formatting_info=True)
        self.sheet = self.old_excel.sheet_by_index(0)
        self.old_rows = self.sheet.nrows
        self.old_cols = self.sheet.ncols
        self.salary = []

    # 获取考勤数据，天数，请假，休息
    def getDays(self,_col=2):
        for r in range(self.old_rows):
            # print(sheet.row_values(r))
            gz = self.sheet.cell(r, _col).value
            self.salary.append(gz)

    # 计算工资
    def calc_salary(self):
        self.salary[0]="实发工资"
        for i,gz in enumerate(self.salary):
            if gz != '实发工资':
                self.salary[i]= 6000 / 30 * self.salary[i]
        print('工资列表')
        print(self.salary)

    def write_new(self,_filename ='test1.xls'):
        new_excel = copy(self.old_excel)
        ws = new_excel.get_sheet(0)

        for i in range(self.old_rows):
            print(i)
            ws.write(i, self.old_cols, self.salary[i])
        new_excel.save(_filename)

s = Salary('test1.xls')
s.getDays(2)
s.calc_salary()
s.write_new()

